<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/08/09
 * Time: 下午15:45
 */

class LiveBroadcastController extends BaseController{

    /**
     * @Author:          sqq
     * @DateTime:        2018-08-09 下午15:46
     * @Description:     api3.0 直播
     * @param string $token             用户token
     * @param int $order_id                 订单号
     * @link http://www.activity.com/wechat/api3.0/Live_broadcast?token=668c155fab8dd6ef1e789b33ff6d040e&order_id=15338054528553
     */
    public function index(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $order_id = Input::get('order_id');

            $response = LiveAccessToken::LiveBroadcast($users->id,$order_id);
            $order = Orders::where('user_id',$users->id)->where('order_id',$order_id)->select('order_id','status','reception_id','reception_name','shop_id')->first();
            if(!is_null($order)){
                $response['data'][0]['order_id'] = $order->order_id;
                $response['data'][0]['order_status'] = $order->status;
                $response['data'][0]['status_chiness'] = Orders::$status[$order->status];
                $response['data'][0]['reception_id'] = $order->reception_id;
                $response['data'][0]['reception_name'] = $order->reception_name;

                $shops = MaintenanceShops::find($order->shop_id);
                if(!is_null($shops)){
                    $response['data'][0]['tel_phone'] = $shops->tel_phone;
                }

                $_status = '';
                foreach(Orders::$order_status as $key => $value){
                    if(in_array($order->status,$value)){
                        if($order->status == 1417){
                            $_status = 5;
                        }else{
                            $_status = $key;
                        }
                    }
                }
                $response['data'][0]['type'] = $_status;

                //获取直播评论
                $feedback = WechatFeedback::where('created_by',$users->id)->where('type',2)->where('order_id',$order_id)->get();
                $response['data'][0]['feedback'] = $feedback;
            }
            if($response['code'] != 200 && $response['code'] != 30002){
                $response['msg'] = '当前设备升级中，暂无法观看';
            }
            //}
            //$response = array('code' => 0, 'msg' => 'ok', 'result' => $response);
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "/LiveBroadcast/index", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:          sqq
     * @DateTime:        2018-08-10 上午11:33
     * @Description:     api3.0 直播评论
     * @param string $token             用户token
     * @param int $order_id                 订单号
     * @link： http://www.activity.com/wechat/api3.0/addLiveComment?token=668c155fab8dd6ef1e789b33ff6d040e&order_id=15338054528553&content=sqq
     */
    public function addLiveComment(){
        header("Content-type:text/html;charset=utf-8");
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'content' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $order_id = Input::get('order_id');
            $content = Input::get('content');

            $feedback = new WechatFeedback();
            $feedback->content = $content;
            $feedback->created_by = $users->id;
            $feedback->created_at = Common::currentTimeString();
            $feedback->updated_at = Common::currentTimeString();
            $feedback->status = 1;
            $feedback->type = 2;
            $feedback->order_id =$order_id;
            if($feedback->save()){
                $response = array('code' => 0, 'msg' => 'ok');
            }else{
                $response = Lang::get("error.add_LiveComment_error_30001");
            }
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "/LiveBroadcast/addLiveComment", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:          sqq
     * @DateTime:        2018-08-17 下午15:27
     * @Description:     api3.0 直播评论列表
     * @param string $token             用户token
     * @param int $order_id                 订单号
     * @link： http://www.activity.com/wechat/api3.0/Live_comment?token=301fa556cf7509819b36db43fb4df384&order_id=15344757922843
     */
    public function Live_comment(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $order_id = Input::get('order_id');

            $order = Orders::where('user_id',$users->id)->where('order_id',$order_id)->select('order_id','status','reception_id','reception_name','shop_id')->first();
            if(!is_null($order)){
                //获取直播评论
                $feedback = WechatFeedback::where('created_by',$users->id)->where('type',2)->where('order_id',$order_id)->get();
                $response['result'] = $feedback;
            }
            //$response = array('code' => 0, 'msg' => 'ok', 'result' => $response);
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "/LiveBroadcast/Live_comment", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}